package com.example.feidaolumanager.componant

import android.content.Context
import android.graphics.Color
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Divider
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import com.bigkoo.pickerview.builder.OptionsPickerBuilder

@Composable
fun ColumnScope.MyBottomSelected(
    context: Context,
    label: String,
    list1: SnapshotStateList<String>,
    list2: SnapshotStateList<MutableList<String>>,
    list3: SnapshotStateList<MutableList<MutableList<String>>>,
    field: String,
    onSelected: (String, Int, Int, Int) -> Unit
) {

    var textView: TextView by remember {
        mutableStateOf(TextView(context))
    }

    Row(modifier = Modifier.align(Alignment.Start)) {
        Text(
            text = label,
            modifier = Modifier.padding(start = 14.dp)
        )
    }

    val keyboardController = LocalSoftwareKeyboardController.current
    AndroidView(
        factory = {
            // 这里是用于初始化View
            textView = TextView(it)
            textView.hint = "点击选择$label"
            textView
        },
        modifier = Modifier
            .fillMaxWidth()
            .padding(all = 10.dp)
            .clickable {
                // 隐藏输入法
                keyboardController?.hide()
                val TAG = this.toString()
                Log.i(TAG, "onCreate: ${list1.size}")
                Log.i(TAG, "onCreate: ${list2.size}")
                Log.i(TAG, "onCreate: ${list3.size}")

                val optionsPickerView = OptionsPickerBuilder(
                    context
                ) { options1: Int, options2: Int, options3: Int, _: View ->
                    //返回的分别是三个级别的选中位置
                    Log.i(TAG, "MyBottomSelected: $options1,$options2,$options3")
                    Log.i(TAG, "onCreate: $list1")
                    Log.i(TAG, "onCreate: $list2")
                    Log.i(TAG, "onCreate: $list3")
                    Log.i(TAG, "onCreate: ${list3[options1][options2].size}")

                    var temp =
                        list1[options1] + "/" + list2[options1][options2] + "/" + list3[options1][options2][options3]

                    // 去除尾部所有的'/'字符
                    while (temp.last() == '/') {
                        temp = temp.removeSuffix("/")
                    }


                    onSelected(temp, options1, options2, options3)
                }.build<String>()

                optionsPickerView.setPicker(
                    list1,
                    list2.toList(),
                    list3.toList()
                )
                optionsPickerView.show(textView, false)
            }) {
        // 这里是用于更新view，因为文本有可能在业务中设置多次，所以写在更新部分
        it.text = field
        it.setTextColor(Color.BLACK)
    }

    Divider()
}
